<!--
  运营-招聘管理-【职位应聘】
 -->
<template>
  <div class="app-container">
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane :label="labelName" name="1">
        <!-- 搜索工作栏 -->
        <el-form
          ref="leftQueryForm"
          :model="leftQueryParams"
          size="small"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="职位名称" prop="jobName">
            <el-input
              v-model="leftQueryParams.jobName"
              placeholder="请输入职位名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="企业名称" prop="companyName">
            <el-input
              v-model="leftQueryParams.companyName"
              placeholder="请输入企业名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="职位状态" prop="jobStatus">
            <el-select v-model="leftQueryParams.jobStatus">
              <el-option
                v-for="(item, ind) in jobHireStatusArray"
                :key="ind"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-refresh"
              @click="resetQuery"
            >重置</el-button>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleQuery"
            >搜索</el-button>
          </el-form-item>
        </el-form>
        <!-- 列表 -->
        <el-table
          v-loading="loading"
          :data="leftList"
          @sort-change="
            ({ column, prop, order }) =>
              sortChange('left', { column, prop, order })
          "
        >
          <el-table-column label="序号" type="index" />
          <el-table-column label="职位名称" align="center" prop="jobName" />
          <el-table-column label="招聘企业" align="center" prop="companyName" />
          <el-table-column
            label="简历投递数量"
            align="center"
            prop="resumeCount"
          >
            <template slot-scope="scope">
              <el-button type="text" @click="openPage(scope.row)">{{
                scope.row.resumeCount || "0"
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column
            label="投递时间"
            align="center"
            prop="deliveryDate"
            width="180"
            sortable
          >
            <template slot-scope="scope">
              <span>{{
                scope.row.deliveryDate
                  ? moment(scope.row.deliveryDate).format("YYYY-MM-DD")
                  : "--"
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="更新日期"
            align="center"
            prop="updateDate"
            sortable
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.updateDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="职位状态" align="center" prop="jobStatus">
            <template slot-scope="scope">
              {{ jobHireStatus[scope.row.jobStatus] || "--" }}
            </template>
          </el-table-column>
          <el-table-column label="部门" align="center" prop="department" />
          <el-table-column
            label="工作地址"
            align="center"
            prop="fullWorkAddress"
          />
        </el-table>
        <!-- 分页组件 -->
        <pagination
          v-show="leftTotal > 0"
          :total="leftTotal"
          :page.sync="leftQueryParams.pageNo"
          :limit.sync="leftQueryParams.pageSize"
          @pagination="leftGetList"
        />
      </el-tab-pane>
      <el-tab-pane label="简历列表" name="2">
        <!-- 搜索工作栏 -->
        <el-form
          ref="rightQueryForm"
          :model="rightQueryParams"
          size="small"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="投递日期" prop="date">
            <el-date-picker
              v-model="rightQueryParams.date"
              type="daterange"
              range-separator="至"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              size="small"
              @change="rightGetList"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="rightQueryParams.status">
              <el-option
                v-for="(item, ind) in resumeStatus"
                :key="ind"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="学生名称" prop="studentName">
            <el-input
              v-model="rightQueryParams.studentName"
              placeholder="请输入学生名称"
              clearable
            />
          </el-form-item>
          <el-form-item label="来源" prop="source">
            <el-select v-model="rightQueryParams.source">
              <el-option
                v-for="(item, ind) in resumeSource"
                :key="ind"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="收藏" prop="collectStatus">
            <el-select v-model="rightQueryParams.collectStatus">
              <el-option label="不限" :value="null" />
              <el-option label="是" value="1" />
              <el-option label="否" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="求职状态" prop="jobStatus">
            <el-select v-model="rightQueryParams.jobStatus">
              <el-option label="不限" :value="null" />
              <el-option label="正在实习" value="正在实习" />
              <el-option label="暂无实习" value="暂无实习" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-refresh"
              @click="resetQuery"
            >重置</el-button>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleQuery"
            >搜索</el-button>
          </el-form-item>
        </el-form>
        <!-- 列表 -->
        <el-table
          v-loading="loading"
          :data="rightList"
          @sort-change="
            ({ column, prop, order }) =>
              sortChange('right', { column, prop, order })
          "
        >
          <el-table-column label="序号" type="index" />
          <el-table-column label="应聘职位" align="center" prop="jobName" />
          <el-table-column label="企业名称" align="center" prop="companyName" />
          <el-table-column label="应聘学生" align="center" prop="studentName" />
          <el-table-column label="学校" align="center" prop="schoolName" />
          <el-table-column label="居住地" align="center" prop="liveCity" />
          <el-table-column
            label="投递日期"
            align="center"
            prop="postDate"
            width="180"
            sortable
          >
            <template slot-scope="scope">
              <span>{{ moment(scope.row.postDate).format("yyyy-MM-DD") }}</span>
            </template>
          </el-table-column>

          <el-table-column label="来源" align="center" prop="source">
            <template slot-scope="scope">
              {{ getSource(scope.row.source) || "--" }}
            </template>
          </el-table-column>
          <el-table-column label="收藏" align="center" prop="collectStatus">
            <template slot-scope="scope">
              {{ ["否", "是"][scope.row.collectStatus] || "否" }}
            </template>
          </el-table-column>
          <el-table-column label="最新备注" align="center" prop="remarks">
            <template slot-scope="scope">
              {{ scope.row.remarks || "--" }}
            </template>
          </el-table-column>
          <el-table-column label="求职状态" align="center" prop="jobStatus">
            <template slot-scope="scope">
              <span v-if="scope.row.jobStatus">{{ scope.row.jobStatus }}/</span>
              <span>{{ scope.row.inJobTime || "--" }}</span>
            </template>
          </el-table-column>

          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              {{ getDeliverStatus(scope.row.status) }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleCheck(scope.row)"
              >查看</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination
          v-show="rightTotal > 0"
          :total="rightTotal"
          :page.sync="rightQueryParams.pageNo"
          :limit.sync="rightQueryParams.pageSize"
          @pagination="rightGetList"
        />
      </el-tab-pane>
    </el-tabs>
    <!-- 查看应聘详情 -->
    <el-drawer
      title="应聘详情"
      size="90%"
      :visible.sync="detailDialog"
      direction="rtl"
    >
      <ResumeDetail
        v-if="currentRow && currentRow.jobId && detailDialog"
        ref="resumeDetail"
        :user-info="currentRow"
        :job-id="currentRow.jobId"
        :resume-id="currentRow.resumeId"
        :invite-recored-id="currentRow.id"
        @saveCollectFun="saveCollectFun"
      />
    </el-drawer>
    <!-- 职位下的简历列表 -->
    <el-drawer
      title="简历列表"
      size="90%"
      :visible.sync="resumeDialog"
      direction="rtl"
    >
      <ResumeList
        v-if="resumeDialog"
        ref="ResumeList"
        @checkDetail="handleCheck"
      />
    </el-drawer>
  </div>
</template>

<script>
import {
  getCheckInfoByJob,
  checkResumeFromJob
} from '@/api/school-admin-api/hireManage/index.js'
import {
  businessAreaData,
  getDeliverStatus,
  jobHireStatus,
  jobStatus,
  resumeStatus,
  jobHireStatusArray
} from '@/utils/frontDataDict.js'
import { deepClone } from '@/utils/index'
import moment from 'moment'
import ResumeList from './resumeList.vue'
import ResumeDetail from './resumeDetail.vue'
export default {
  name: 'Company',
  components: {
    ResumeList,
    ResumeDetail
  },
  props: {
    labelName: {
      // 职位id
      type: String,
      default: '按职位查看'
    }
  },
  data() {
    return {
      moment: moment,
      jobHireStatusArray: jobHireStatusArray,
      jobHireStatus: jobHireStatus,
      getDeliverStatus: getDeliverStatus,
      resumeStatus: resumeStatus,
      resumeSource: [
        {
          label: '不限',
          value: null
        },
        {
          label: '自主投递',
          value: '1'
        },
        {
          label: '平台推荐',
          value: '2'
        },
        {
          label: '企业邀请',
          value: '3'
        }
      ],
      currentRow: null,
      detailDialog: false, // 应聘详情
      resumeDialog: false, // 简历详情
      activeCheck: 'jobFrom',
      reason: '',
      auditDialog: false,
      checkResult: '1',
      activeName: '1',
      jobStatus: jobStatus, // 职位上下架状态
      // 企业行业类型数据，二级联动
      businessAreaData: businessAreaData,
      // 遮罩层
      loading: true,
      // 职位查看-总条数
      leftTotal: 0,
      // 简历列表-总条数
      rightTotal: 0,
      // 职位查看列表
      leftList: [],
      // 简历列表列表
      rightList: [],
      // 弹出层标题
      title: '',
      // 职位查看查询参数
      leftQueryParams: {
        pageNo: 1,
        pageSize: 10,
        jobName: null,
        companyName: null,
        jobStatus: null,
        sortField: {
          field: '',
          order: ''
        }
      },
      rightQueryParams: {
        pageNo: 1,
        pageSize: 10,
        jobName: null,
        date: null,
        studentName: null,
        status: null,
        source: null,
        collectStatus: null,
        jobStatus: null,
        sortField: {
          field: '',
          order: ''
        }
      }
    }
  },
  created() {
    this.leftGetList()
  },
  methods: {
    getSource(value) {
      if (!value) return '--'
      return this.resumeSource.filter((v) => v.value == value)[0].label
    },
    sortChange(type, { column, prop, order }) {
      const sortType = {
        ascending: 'asc',
        descending: 'desc'
      }
      const sortField = {
        field: prop,
        order: order !== null ? sortType[order] : null
      }
      if (order === null) {
        switch (type) {
          case 'left':
            Object.assign(this.leftQueryParams.sortField, {
              field: '',
              order: ''
            })
            this.leftGetList()
            break
          case 'right':
            Object.assign(this.rightQueryParams.sortField, {
              field: '',
              order: ''
            })
            this.rightGetList()
            break
          default:
            break
        }
      } else {
        switch (type) {
          case 'left':
            Object.assign(this.leftQueryParams.sortField, sortField)
            this.leftGetList()
            break
          case 'right':
            Object.assign(this.rightQueryParams.sortField, sortField)
            this.rightGetList()
            break
          default:
            break
        }
      }
    },
    openPage(row) {
      localStorage.setItem('jobObjective-jobId', row.jobId)
      row.resumeCount && (this.resumeDialog = true)
    },
    // tag切换
    handleClick(data) {
      const index = Number(this.activeName)
      if (index === 1) {
        this.leftGetList()
      } else {
        this.rightGetList()
      }
    },
    // 发布中列表数据
    leftGetList() {
      this.loading = true
      // 处理查询参数
      const params = JSON.parse(JSON.stringify(this.leftQueryParams))
      for (const key in params) {
        if (params[key] === null || params[key] === '') {
          delete params[key]
        }
      }
      if (params.sortField.order === '') {
        delete params.sortField
      }
      // 执行查询
      getCheckInfoByJob(params)
        .then((res) => {
          if (res.code === 0 && res.data) {
            res.data.list = res.data.list.map((item) => {
              try {
                const workCIty = JSON.parse(item.workCIty)
                if (workCIty[0] === workCIty[1]) {
                  // 临时变量，工作地址
                  item.fullWorkAddress = item.workCIty[1]
                } else {
                  // 临时变量，工作地址
                  item.fullWorkAddress =
                    JSON.parse(item.workCIty).join('') || '--'
                }
              } catch (error) {
                item.fullWorkAddress = '--'
              }
              return item
            })
            this.leftList = res.data.list
            this.leftTotal = res.data.total
          }
          this.loading = false
        })
        .catch((e) => {
          this.loading = false
        })
    },
    // 未发布列表数据
    rightGetList() {
      this.loading = true
      // 处理查询参数
      const params = JSON.parse(JSON.stringify(this.rightQueryParams))
      if (Array.isArray(params.date) && params.date.length === 2) {
        params.beginPostDate = params.date[0] + ''
        params.endPostDate = params.date[1] + ''
      }
      delete params.date
      if (params.sortField.order === '') {
        delete params.sortField
      }
      // 执行查询
      checkResumeFromJob(params)
        .then((response) => {
          const list = response.data.list.map((item) => {
            // try {
            //   const education = JSON.parse(item.educationalExperience) ? JSON.parse(item.educationalExperience)[0] : {}
            //   item.schoolName = education?.schoolName || '--'
            //   item.major = education?.major || '--'
            //   item.educationalBackground = education.educationalBackground || '--'
            // } catch (error) {
            //   item.schoolName = '--'
            //   item.major = '--'
            //   item.educationalBackground = '--'
            // }
            if (
              Array.isArray(item.educationalExperience) &&
              item.educationalExperience.length
            ) {
              const educate = ['大专', '本科', '硕士', '博士']
              educate.map((iten) => {
                const matchEducate = item.educationalExperience.find(
                  (single) => single.educationalBackground.indexOf(iten) > -1
                )
                if (matchEducate) {
                  item.schoolName = matchEducate.schoolName // 学校名称
                  item.major = matchEducate.major // 专业
                  item.education = iten // 学历
                }
              })
            } else {
              item.schoolName = '--' // 学校名称
              item.major = '--' // 专业
              item.education = '--' // 学历
            }
            return item
          })
          this.rightList = list
          this.rightTotal = response.data.total
          this.loading = false
        })
        .catch((e) => {
          this.loading = false
        })
    },
    /** 搜索按钮操作 */
    handleQuery(type = true) {
      if (this.activeName === '1') {
        this.leftQueryParams.pageNo = 1
        this.leftGetList()
      } else {
        this.rightQueryParams.pageNo = 1
        this.rightGetList()
      }
    },
    // 重制搜索条件
    resetQuery() {
      if (this.activeName === '1') {
        this.resetForm('leftQueryForm')
        this.handleQuery()
      } else {
        this.resetForm('rightQueryForm')
        this.handleQuery()
      }
    },
    /** 打开应聘详情 */
    handleCheck(row) {
      this.currentRow = deepClone(row)
      this.currentRow.relationType = 1
      setTimeout(() => {
        this.detailDialog = true
        setTimeout(() => {
          this.$refs.resumeDetail.init()
        })
      })
    },
    saveCollectFun() {
      if (this.activeName === '1') {
        this.$refs.ResumeList.getList()
      } else {
        this.rightGetList()
      }
    }
  }
}
</script>
